<?php

class Page extends CI_Controller {

    private $admin;

    function page() {
        parent::__construct();
        $adminname = $this->session->userdata('adminname');
        $adminid = $this->session->userdata('id') != '';
        if ($adminname != '' && $adminid != '') {
            $hash = $this->session->userdata('hash');
            if (md5(SECRETCODE . $adminid . $adminname . SECRETCODE) == $hash) {
                $this->admin['name'] = $adminname;
                //$this->load->view('admin/pages');
                $this->admin['id'] = $adminid;
                $this->admin['hash'] = $hash;
            } else {
                $this->admin['name'] = '';
                $this->admin['id'] = '';
            }
        } else {
            $this->admin['name'] = '';
            $this->admin['id'] = '';
        } 
    }

    function index() {
        
        //die($this->admin['name'].' '.$this->admin['id'].' '.$this->admin['hash'].' '.md5(SECRETCODE .$this->admin['id']. $this->admin['name'] . SECRETCODE));
        
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            // die('avcacasc');
            $this->load->view('admin/common/header');
            $this->load->view('admin/common/footer');
        } else
            redirect(base_url() . 'admin/login');
         
    }

    function login() {
        if ($this->input->post('login')) {
            $username = $this->db->escape_str($this->input->post('username'));
            $password = $this->input->post('password');

            $this->db->where('adminname', $username);
            $query = $this->db->get('admin');
            if ($query->num_rows()) {
                $user = $query->first_row();
                if (md5($password) == $user->password) {
                    $this->session->set_userdata(array(
                        'adminname' => $user->adminname,
                        'id' => $user->id,
                        'hash' => md5(SECRETCODE . $user->id . $user->adminname . SECRETCODE)
                    ));
                    //die('login ok');
                    redirect(base_url() . 'admin');
                } else {
                    $this->load->view('admin/login');
                }
            } else {
                $this->load->view('admin/login');
            }
        } else {
            $adminname = $this->session->userdata('adminname');
            $adminid = $this->session->userdata('id') != '';
            if ($adminname != '' && $adminid != '') {
                $hash = $this->session->userdata('hash');
                if (md5(SECRETCODE . $adminid . $adminname . SECRETCODE) == $hash) {
                    $this->admin['name'] = $adminname;
                    //$this->load->view('admin/index');
                    $this->admin['id'] = $adminid;
                    $this->admin['hash'] = $hash;
                    redirect(base_url() . 'admin');
                } else {
                    $this->admin['name'] = '';
                    $this->admin['id'] = '';
                    $this->load->view('admin/login');
                }
            } else {
                $this->admin['name'] = '';
                $this->admin['id'] = '';
                $this->load->view('admin/login');
            }
        }
    }

    function logout() {
        $this->session->sess_destroy();
        redirect(base_url() . 'admin/login');
    }
}
